package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.maiji.cloud.request.shopingmall.ParamConfigReqData;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.shopingmall.GoodsParamConfigResData;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.shopingmall.GoodsParamConfigEntity;
import com.maiji.cloud.mapper.GoodsParamConfigMapper;
import com.maiji.cloud.service.GoodsParamConfigService;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class GoodsParamConfigServiceImpl extends ServiceImpl<GoodsParamConfigMapper, GoodsParamConfigEntity> implements GoodsParamConfigService{

    @Override
    public BaseDataResDto<List<GoodsParamConfigResData>> getGoodsParamById(String goodsId) {
        EntityWrapper<GoodsParamConfigEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("goods_id", goodsId);
        entityWrapper.orderBy("sort", true);
        List<GoodsParamConfigResData> listGoodsParamConfigResData = selectList(entityWrapper)
                .parallelStream().filter(gpc -> StringUtil.isNotBlank(gpc.getValue())).map(goodsParamConfigEntity ->{
                    GoodsParamConfigResData goodsParamConfigResData = new GoodsParamConfigResData();
                    BeanUtils.copyProperties(goodsParamConfigEntity, goodsParamConfigResData);
                    return goodsParamConfigResData;
                }).collect(Collectors.toList());
        return new BaseDataResDto<List<GoodsParamConfigResData>>(Status.SUCCESS).setData(listGoodsParamConfigResData);
    }

    @Override
    public Boolean editGoodsParamByGoodsId(String goodsId, List<ParamConfigReqData> params) {
        delete(new EntityWrapper<GoodsParamConfigEntity>().eq("goods_id", goodsId));
        List<GoodsParamConfigEntity> listGoodsParamConfigEntity = Lists.newArrayList();
        for(ParamConfigReqData paramConfigReqData : params) {
            GoodsParamConfigEntity goodsParamConfigEntity = new GoodsParamConfigEntity();
            goodsParamConfigEntity.setValue(paramConfigReqData.getValue());
            goodsParamConfigEntity.setName(paramConfigReqData.getName());
            goodsParamConfigEntity.setSort(paramConfigReqData.getSort());
            goodsParamConfigEntity.setGoodsId(goodsId);
            goodsParamConfigEntity.setUuId(UUID_MD5.getUUID());
            goodsParamConfigEntity.setParamConfigId(paramConfigReqData.getUuId());
            listGoodsParamConfigEntity.add(goodsParamConfigEntity);
        }
        return insertBatch(listGoodsParamConfigEntity);
    }

}
